package top.chengdongqing.mishop.common.security

import org.springframework.security.authentication.AuthenticationProvider
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
import org.springframework.security.core.Authentication
import org.springframework.stereotype.Component
import top.chengdongqing.mishop.utils.JwtUtils

@Component
class JwtAuthenticationProvider(
    private val jwtUtils: JwtUtils
) : AuthenticationProvider {

    override fun authenticate(authentication: Authentication): Authentication {
        val token = authentication.principal as String
        val userId = jwtUtils.getUserId(token)
        return UsernamePasswordAuthenticationToken.authenticated(userId, token, listOf())
    }

    override fun supports(authentication: Class<*>): Boolean {
        return UsernamePasswordAuthenticationToken::class.java.isAssignableFrom(authentication)
    }
}